using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using PointOfSale.Global;
using PointOfSale.Class;
using System.Text.RegularExpressions;

namespace PointOfSale.Configuration.Item
{
	public class frmSetupStaple : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.LookupComboBox cboItem;
		private System.Windows.Forms.LookupComboBox cboStaple;
		private System.Windows.Forms.TextBox txtQuantity;
		private System.Windows.Forms.LookupComboBox cboUnit;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnStapleAdd;
		private System.Windows.Forms.Button btnStapleDelete;
		private System.Windows.Forms.Button btnStapleUpdate;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnUpdate;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.ListView lvStaple;
		private System.Windows.Forms.ListView lvItem;
		private System.Windows.Forms.ColumnHeader clhStapleId;
		private System.Windows.Forms.ColumnHeader clhStapleName;
		private System.Windows.Forms.ColumnHeader clhQuantity;
		private System.Windows.Forms.ColumnHeader clhUnitId;
		private System.Windows.Forms.ColumnHeader clhUnitName;
		private System.Windows.Forms.ColumnHeader clhItemId;
		private System.Windows.Forms.ColumnHeader clhItemName;
		private System.Windows.Forms.ColumnHeader clhRecipe;
		private DataTable dtStaple;
		private bool isFormLoading;
		private System.Windows.Forms.ComboBox cboDonViChuan;
		private System.Windows.Forms.Label lblDonViChuan;
		private int stapleId;

		public frmSetupStaple()
		{
			InitializeComponent();

			//Bang nay dung de luu thong tin ve nguyen vat lieu.
			dtStaple = new DataTable ("tblStaple");
			dtStaple.Columns.Add(clsDatabase.CreateNewColumn( "Int32","StapleId","StapleId"));
			dtStaple.Columns.Add(clsDatabase.CreateNewColumn( "String","StapleName","StapleName"));
			dtStaple.Columns.Add(clsDatabase.CreateNewColumn( "Double","Quantity","Quantity"));
			dtStaple.Columns.Add(clsDatabase.CreateNewColumn( "Int32","UnitId","UnitId"));
			dtStaple.Columns.Add(clsDatabase.CreateNewColumn( "String","UnitName","UnitName"));
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code

		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.btnStapleUpdate = new System.Windows.Forms.Button();
			this.btnStapleDelete = new System.Windows.Forms.Button();
			this.btnStapleAdd = new System.Windows.Forms.Button();
			this.lvStaple = new System.Windows.Forms.ListView();
			this.clhStapleId = new System.Windows.Forms.ColumnHeader();
			this.clhStapleName = new System.Windows.Forms.ColumnHeader();
			this.clhQuantity = new System.Windows.Forms.ColumnHeader();
			this.clhUnitId = new System.Windows.Forms.ColumnHeader();
			this.clhUnitName = new System.Windows.Forms.ColumnHeader();
			this.label1 = new System.Windows.Forms.Label();
			this.cboUnit = new System.Windows.Forms.LookupComboBox();
			this.txtQuantity = new System.Windows.Forms.TextBox();
			this.cboStaple = new System.Windows.Forms.LookupComboBox();
			this.lvItem = new System.Windows.Forms.ListView();
			this.clhItemId = new System.Windows.Forms.ColumnHeader();
			this.clhItemName = new System.Windows.Forms.ColumnHeader();
			this.clhRecipe = new System.Windows.Forms.ColumnHeader();
			this.cboItem = new System.Windows.Forms.LookupComboBox();
			this.btnAdd = new System.Windows.Forms.Button();
			this.btnUpdate = new System.Windows.Forms.Button();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.cboDonViChuan = new System.Windows.Forms.ComboBox();
			this.lblDonViChuan = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.btnStapleUpdate);
			this.groupBox1.Controls.Add(this.btnStapleDelete);
			this.groupBox1.Controls.Add(this.btnStapleAdd);
			this.groupBox1.Controls.Add(this.lvStaple);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.cboUnit);
			this.groupBox1.Controls.Add(this.txtQuantity);
			this.groupBox1.Controls.Add(this.cboStaple);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 40);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(680, 208);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Các thành phần";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(432, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 16);
			this.label4.TabIndex = 9;
			this.label4.Text = "Đơn vị";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(352, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 16);
			this.label3.TabIndex = 8;
			this.label3.Text = "Số lượng";
			// 
			// btnStapleUpdate
			// 
			this.btnStapleUpdate.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnStapleUpdate.Location = new System.Drawing.Point(592, 128);
			this.btnStapleUpdate.Name = "btnStapleUpdate";
			this.btnStapleUpdate.Size = new System.Drawing.Size(80, 32);
			this.btnStapleUpdate.TabIndex = 7;
			this.btnStapleUpdate.Text = "Sửa thành phần";
			this.btnStapleUpdate.Click += new System.EventHandler(this.btnStapleUpdate_Click);
			// 
			// btnStapleDelete
			// 
			this.btnStapleDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnStapleDelete.Location = new System.Drawing.Point(592, 168);
			this.btnStapleDelete.Name = "btnStapleDelete";
			this.btnStapleDelete.Size = new System.Drawing.Size(80, 32);
			this.btnStapleDelete.TabIndex = 6;
			this.btnStapleDelete.Text = "Xóa thành phần";
			this.btnStapleDelete.Click += new System.EventHandler(this.btnStapleDelete_Click);
			// 
			// btnStapleAdd
			// 
			this.btnStapleAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnStapleAdd.Location = new System.Drawing.Point(592, 32);
			this.btnStapleAdd.Name = "btnStapleAdd";
			this.btnStapleAdd.Size = new System.Drawing.Size(80, 32);
			this.btnStapleAdd.TabIndex = 5;
			this.btnStapleAdd.Text = "Thêm thành phần";
			this.btnStapleAdd.Click += new System.EventHandler(this.btnStapleAdd_Click);
			// 
			// lvStaple
			// 
			this.lvStaple.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this.clhStapleId,
																					   this.clhStapleName,
																					   this.clhQuantity,
																					   this.clhUnitId,
																					   this.clhUnitName});
			this.lvStaple.FullRowSelect = true;
			this.lvStaple.GridLines = true;
			this.lvStaple.HideSelection = false;
			this.lvStaple.Location = new System.Drawing.Point(8, 56);
			this.lvStaple.Name = "lvStaple";
			this.lvStaple.Size = new System.Drawing.Size(576, 152);
			this.lvStaple.TabIndex = 4;
			this.lvStaple.View = System.Windows.Forms.View.Details;
			this.lvStaple.SelectedIndexChanged += new System.EventHandler(this.lvStaple_SelectedIndexChanged);
			// 
			// clhStapleId
			// 
			this.clhStapleId.Text = "StapleId";
			this.clhStapleId.Width = 0;
			// 
			// clhStapleName
			// 
			this.clhStapleName.Text = "Tên thành phần";
			this.clhStapleName.Width = 388;
			// 
			// clhQuantity
			// 
			this.clhQuantity.Text = "Số lượng";
			this.clhQuantity.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.clhQuantity.Width = 88;
			// 
			// clhUnitId
			// 
			this.clhUnitId.Text = "UnitId";
			this.clhUnitId.Width = 0;
			// 
			// clhUnitName
			// 
			this.clhUnitName.Text = "Đơn vị";
			this.clhUnitName.Width = 95;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 16);
			this.label1.TabIndex = 3;
			this.label1.Text = "Tên thành phần";
			// 
			// cboUnit
			// 
			this.cboUnit.AllowTypeAllSymbols = true;
			this.cboUnit.Location = new System.Drawing.Point(432, 32);
			this.cboUnit.Name = "cboUnit";
			this.cboUnit.Size = new System.Drawing.Size(136, 21);
			this.cboUnit.TabIndex = 2;
			this.cboUnit.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboUnit_KeyDown);
			// 
			// txtQuantity
			// 
			this.txtQuantity.Location = new System.Drawing.Point(352, 32);
			this.txtQuantity.Name = "txtQuantity";
			this.txtQuantity.Size = new System.Drawing.Size(72, 20);
			this.txtQuantity.TabIndex = 1;
			this.txtQuantity.Text = "";
			this.txtQuantity.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtQuantity_KeyDown);
			// 
			// cboStaple
			// 
			this.cboStaple.AllowTypeAllSymbols = true;
			this.cboStaple.Location = new System.Drawing.Point(8, 32);
			this.cboStaple.Name = "cboStaple";
			this.cboStaple.Size = new System.Drawing.Size(336, 21);
			this.cboStaple.TabIndex = 0;
			this.cboStaple.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboStaple_KeyDown);
			this.cboStaple.SelectedIndexChanged += new System.EventHandler(this.cboStaple_SelectedIndexChanged);
			// 
			// lvItem
			// 
			this.lvItem.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					 this.clhItemId,
																					 this.clhItemName,
																					 this.clhRecipe});
			this.lvItem.FullRowSelect = true;
			this.lvItem.GridLines = true;
			this.lvItem.HideSelection = false;
			this.lvItem.Location = new System.Drawing.Point(8, 256);
			this.lvItem.Name = "lvItem";
			this.lvItem.Size = new System.Drawing.Size(584, 256);
			this.lvItem.TabIndex = 1;
			this.lvItem.View = System.Windows.Forms.View.Details;
			this.lvItem.Click += new System.EventHandler(this.lvItem_Click);
			this.lvItem.SelectedIndexChanged += new System.EventHandler(this.lvItem_SelectedIndexChanged);
			// 
			// clhItemId
			// 
			this.clhItemId.Text = "ItemId";
			this.clhItemId.Width = 0;
			// 
			// clhItemName
			// 
			this.clhItemName.Text = "Tên mặt hàng";
			this.clhItemName.Width = 222;
			// 
			// clhRecipe
			// 
			this.clhRecipe.Text = "Các thành phần";
			this.clhRecipe.Width = 357;
			// 
			// cboItem
			// 
			this.cboItem.AllowTypeAllSymbols = true;
			this.cboItem.Location = new System.Drawing.Point(104, 8);
			this.cboItem.Name = "cboItem";
			this.cboItem.Size = new System.Drawing.Size(312, 21);
			this.cboItem.TabIndex = 0;
			this.cboItem.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboItem_KeyDown);
			this.cboItem.SelectedIndexChanged += new System.EventHandler(this.cboItem_SelectedIndexChanged);
			// 
			// btnAdd
			// 
			this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnAdd.Location = new System.Drawing.Point(600, 280);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new System.Drawing.Size(80, 32);
			this.btnAdd.TabIndex = 2;
			this.btnAdd.Text = "Thêm mặt hàng";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// btnUpdate
			// 
			this.btnUpdate.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnUpdate.Location = new System.Drawing.Point(600, 320);
			this.btnUpdate.Name = "btnUpdate";
			this.btnUpdate.Size = new System.Drawing.Size(80, 32);
			this.btnUpdate.TabIndex = 3;
			this.btnUpdate.Text = "Sửa mặt hàng";
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			// 
			// btnDelete
			// 
			this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnDelete.Location = new System.Drawing.Point(600, 360);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(80, 32);
			this.btnDelete.TabIndex = 4;
			this.btnDelete.Text = "Xóa mặt hàng";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnOK
			// 
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnOK.Location = new System.Drawing.Point(600, 400);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(80, 32);
			this.btnOK.TabIndex = 5;
			this.btnOK.Text = "Thoát";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "Tên mặt hàng";
			// 
			// cboDonViChuan
			// 
			this.cboDonViChuan.Location = new System.Drawing.Point(520, 8);
			this.cboDonViChuan.Name = "cboDonViChuan";
			this.cboDonViChuan.Size = new System.Drawing.Size(121, 21);
			this.cboDonViChuan.TabIndex = 7;
			this.cboDonViChuan.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboDonViChuan_KeyDown);
			// 
			// lblDonViChuan
			// 
			this.lblDonViChuan.AutoSize = true;
			this.lblDonViChuan.Location = new System.Drawing.Point(432, 8);
			this.lblDonViChuan.Name = "lblDonViChuan";
			this.lblDonViChuan.Size = new System.Drawing.Size(75, 16);
			this.lblDonViChuan.TabIndex = 8;
			this.lblDonViChuan.Text = "§¬n vÞ chuÈn";
			// 
			// frmSetupStaple
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(690, 519);
			this.Controls.Add(this.lblDonViChuan);
			this.Controls.Add(this.cboDonViChuan);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnUpdate);
			this.Controls.Add(this.btnAdd);
			this.Controls.Add(this.lvItem);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.cboItem);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.Name = "frmSetupStaple";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Bảng nguyên vật liệu";
			this.Load += new System.EventHandler(this.frmSetupStaple_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnStapleAdd_Click(object sender, System.EventArgs e)
		{
			bool condition = true;;
			string message = "";
			
			if(cboItem.Text == "")
			{
				condition = false;
				message += clsGlobal.g_Hashtable["ItemNameNull"].ToString() + "\n";
			}
			if(cboStaple.Text == "")
			{
				condition = false;
				message += clsGlobal.g_Hashtable["StapleNameNull"].ToString() + "\n";
			}
			for(int i = 0; i < this.lvStaple.Items.Count; i++)
			{
				if(this.cboStaple.Text == this.lvStaple.Items[i].SubItems[1].Text)
				{
					condition = false;
					message += "Thành phần này đã có rồi \n";
				}
			}
			if(txtQuantity.Text == "")
			{
				condition = false;
				message += clsGlobal.g_Hashtable["QuantityIsNull"].ToString() + "\n";
			}
			Match match = Regex.Match(txtQuantity.Text.Trim(), "[0-9]+[.]*[0-9]*");
			if(match.ToString() != txtQuantity.Text.Trim())
			{
				condition = false;
				message += clsGlobal.g_Hashtable["QuantityIsNull"].ToString() + "\n";
			}
			if(cboUnit.Text =="")
			{
				condition = false;
				message += clsGlobal.g_Hashtable["UnitNameNull"].ToString() + "\n";
			}
			if(condition)
			{
				string stapleId = cboStaple.SelectedValue.ToString();
				string stapleName = cboStaple.Text.Trim();
				string quantity = txtQuantity.Text.Trim();
				string unitId = cboUnit.SelectedValue.ToString();
				string unitName = cboUnit.Text.Trim();

				//Them vao dtStaple
				DataRow drRowStore =  dtStaple.NewRow();
			
				drRowStore["StapleId"] = stapleId;
				drRowStore["StapleName"] = stapleName;
				drRowStore["Quantity"] = Convert.ToDouble(quantity);
				drRowStore["UnitId"] = unitId;
				drRowStore["UnitName"] = unitName;

				dtStaple.Rows.Add (drRowStore);

				//Hien thi len listview
				ListViewItem lsvItem = new ListViewItem(stapleId);
				lsvItem.SubItems.Add(stapleName);
				lsvItem.SubItems.Add(quantity);
				lsvItem.SubItems.Add(unitId);
				lsvItem.SubItems.Add(unitName);
				lvStaple.Items.Add(lsvItem);
				btnAdd.Enabled = true;

				this.cboStaple.Focus();
			}
			else
			{
				MessageBox.Show(message);
			}
		}

		private void btnStapleUpdate_Click(object sender, System.EventArgs e)
		{
			bool condition = true;
			if(txtQuantity.Text == "")
			{
				condition = false;
				MessageBox.Show(clsGlobal.g_Hashtable["QuantityIsNull"].ToString());
			}
			Match match = Regex.Match(txtQuantity.Text.Trim(), "[0-9]+[.]*[0-9]*");
			if(match.ToString() != txtQuantity.Text.Trim())
			{
				condition = false;
				MessageBox.Show(clsGlobal.g_Hashtable["QuantityIsNull"].ToString());
			}
			if(condition)
			{
				this.lvStaple.FocusedItem.SubItems[0].Text = this.cboStaple.SelectedValue.ToString();
				this.lvStaple.FocusedItem.SubItems[1].Text = this.cboStaple.Text;
				this.lvStaple.FocusedItem.SubItems[3].Text = this.cboUnit.SelectedValue.ToString();
				this.lvStaple.FocusedItem.SubItems[4].Text = this.cboUnit.Text;
				this.lvStaple.FocusedItem.SubItems[2].Text = this.txtQuantity.Text;
			}
		}

		private void btnStapleDelete_Click(object sender, System.EventArgs e)
		{
			this.lvStaple.FocusedItem.Remove();
			this.btnStapleDelete.Enabled = false;
			this.btnStapleUpdate.Enabled = false;
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			int n = dtStaple.Rows.Count;
			clsStaple staple = new clsStaple();
			staple.ItemId = Convert.ToInt32(cboItem.SelectedValue.ToString());

			for(int i = 0; i < n; i++)
			{
				staple.StapleId = Convert.ToInt32(dtStaple.Rows[i][0].ToString());
				staple.Quantity = Convert.ToDouble(dtStaple.Rows[i][2].ToString());
				staple.UnitId = Convert.ToInt32(dtStaple.Rows[i][3].ToString());
				staple.Add();
			}

			//Ghi lai day la hang gia cong
			String strUpdate = "UPDATE tblItem SET IsHandMade = 1 WHERE ItemId = " + staple.ItemId;
			clsDatabase.ExecuteSQL(strUpdate, clsGlobal.g_DataConn);

			lvStaple.Items.Clear();
			txtQuantity.Text = "";

			//Cho hien lai tren listview cua item da duoc setup
			lvItem.Items.Clear();
			LoadDataToListView();	
			
			//Load lai cac mat hang chua duoc setup
			DataTable dtItem = clsItem.GetNotYetStapleSetup();
			cboItem.DataSource = dtItem;
			cboItem.DisplayMember = "itemName";
			cboItem.ValueMember = "itemId";
			dtStaple.Rows.Clear();
		}

		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			clsStaple staple = new clsStaple();
			staple.ItemId = Convert.ToInt32(lvItem.FocusedItem.SubItems[0].Text);
			staple.deleteItemStaple();
			string newItemRecipe = "";
			for(int i = 0; i < this.lvStaple.Items.Count; i++)
			{
				staple.StapleId = Convert.ToInt32(this.lvStaple.Items[i].SubItems[0].Text);
				staple.Quantity = Convert.ToDouble(this.lvStaple.Items[i].SubItems[2].Text);
				staple.UnitId = Convert.ToInt32(this.lvStaple.Items[i].SubItems[3].Text);
				newItemRecipe += this.lvStaple.Items[i].SubItems[1].Text + ", ";
				staple.Add();
			}
			if(newItemRecipe.Length > 2)
			{
				newItemRecipe = newItemRecipe.Substring(0, newItemRecipe.Length - 2);
			}
			this.lvItem.FocusedItem.SubItems[2].Text = newItemRecipe;
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			string itemID = this.lvItem.FocusedItem.SubItems[0].Text;
			clsStaple staple = new clsStaple();
			staple.ItemId = Convert.ToInt32(itemID);
			staple.deleteItemStaple();

			string updateCommand = "update tblItem set isHandMade = 0 where itemID = " + itemID;
			clsDatabase.ExecuteSQL(updateCommand, clsGlobal.g_DataConn);

			this.lvItem.FocusedItem.Remove();

			//Dieu chinh hien thi form
			this.btnUpdate.Enabled = false;
			this.btnDelete.Enabled = false;
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void frmSetupStaple_Load(object sender, System.EventArgs e)
		{
			isFormLoading = true;

			DataTable dtItem = clsItem.GetNotYetStapleSetup();
			cboItem.DataSource = dtItem;
			cboItem.DisplayMember = "itemName";
			cboItem.ValueMember = "itemId";

			DataTable tatCaDonVi = clsUnit.GetAll();
			this.cboDonViChuan.DataSource = tatCaDonVi;
			this.cboDonViChuan.DisplayMember = "unitName";
			this.cboDonViChuan.ValueMember = "unitId";

			//Lay du lieu tu co so du lieu vao trong cbo itemname
			DataTable dtStaple = clsItem.GetByDepartment(Convert.ToInt32(clsGlobal.g_DepartmentId));

			cboStaple.DataSource = dtStaple;
			cboStaple.DisplayMember = "ItemName";
			cboStaple.ValueMember = "ItemId";
			stapleId = Convert.ToInt32(cboStaple.SelectedValue.ToString());

			cboUnit.DataSource = clsUnitConversion.GetUnitForAnItem(stapleId);
			cboUnit.DisplayMember = "unitName";
			cboUnit.ValueMember = "unitId";



			btnStapleUpdate.Enabled = false;
			btnStapleDelete.Enabled = false;

			//btnAdd.Enabled = false;
			btnUpdate.Enabled = false;
			btnDelete.Enabled = false;

			LoadDataToListView();
			isFormLoading = false;
		}
		private void LoadDataToListView()
		{
			DataTable dtItem = clsItem.GetAllSetupItem();
			int n  = dtItem.Rows.Count;
			for(int i = 0; i < n; i++)
			{
				string itemID = dtItem.Rows[i][0].ToString();
				string itemRecipe = clsStaple.getItemRecipe(itemID);
				ListViewItem lsvItem = new ListViewItem(new string[]{itemID, dtItem.Rows[i][1].ToString(), itemRecipe});
				lsvItem.SubItems.Add(dtItem.Rows[i][1].ToString());
				lvItem.Items.Add(lsvItem);
			}
		}

		private void lvItem_Click(object sender, System.EventArgs e)
		{
		
		}

		private void lvItem_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			//Hien thi cac thanh phan tren bang thanh phan
			this.lvStaple.Items.Clear();
			int selectedItemID = Convert.ToInt32(this.lvItem.FocusedItem.SubItems[0].Text);
			DataTable danhSachThanhPhan = clsStaple.DanhSachThanhPhan(selectedItemID);
			for(int i = 0; i < danhSachThanhPhan.Rows.Count; i++)
			{
				string stapleName = danhSachThanhPhan.Rows[i]["itemName"].ToString();
				string unitName = danhSachThanhPhan.Rows[i]["unitName"].ToString();
				string qty = danhSachThanhPhan.Rows[i]["qty"].ToString();
				string stapleID = danhSachThanhPhan.Rows[i]["stapleID"].ToString();
				string unitID = danhSachThanhPhan.Rows[i]["unitID"].ToString();
				ListViewItem lsvItem = new ListViewItem(new string[]{stapleID, stapleName, qty, unitID, unitName});
				this.lvStaple.Items.Add(lsvItem);
			}
			
			//Hien thi o cac thanh phan khac tren form
			this.cboItem.Text = this.lvItem.FocusedItem.SubItems[1].Text;

			//Dieu chinh hien thi tren form
			this.btnUpdate.Enabled = true;
			this.btnDelete.Enabled = true;
		}

		private void lvStaple_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			//Hien thi cac thanh phan cho phep chinh sua thong tin
			this.cboStaple.Text = this.lvStaple.FocusedItem.SubItems[1].Text;
			this.txtQuantity.Text = this.lvStaple.FocusedItem.SubItems[2].Text;
			this.cboUnit.Text = this.lvStaple.FocusedItem.SubItems[4].Text;

			//Dieu chinh hien thi form
			this.btnStapleUpdate.Enabled = true;
			this.btnStapleDelete.Enabled = true;
		}

		private void cboStaple_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(!isFormLoading)
			{
				try
				{
					cboStaple.ValueMember = "ItemId";
					//lay ItemId
					stapleId = Convert.ToInt32(cboStaple.SelectedValue.ToString());

					cboUnit.DataSource = clsUnitConversion.GetUnitForAnItem(stapleId);
					cboUnit.DisplayMember = "UnitName";
					cboUnit.ValueMember = "UnitId";
					
				}
				catch(Exception exp)
				{
					MessageBox.Show(exp.ToString());
				}
			}
		}

		private void cboItem_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboDonViChuan.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}

		private void cboStaple_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.txtQuantity.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}

		private void txtQuantity_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboUnit.Focus();
			}
		}

		private void cboUnit_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.btnStapleAdd.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}

		private void cboItem_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.cboItem.DisplayMember == "itemName" && this.cboItem.ValueMember == "itemId")
			{
				int itemId = Convert.ToInt32(this.cboItem.SelectedValue);
				DataTable danhSachDonViCuaMatHang = clsUnitConversion.GetUnitForAnItem(itemId);
				this.cboDonViChuan.DataSource = danhSachDonViCuaMatHang;
				this.cboDonViChuan.DisplayMember = "unitName";
				this.cboDonViChuan.ValueMember = "unitId";
			}
		}

		private void cboDonViChuan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboStaple.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}
	}
}
